#include "send_msg_handle.h"

u8 temp_buf[BUF_SIZE];
static void print_data_hex(uint8_t *data, u16 len)
{   
    for (int i = 0; i < len; i++) 
    {
        printf(" %02x", data[i]);
    }
    printf(", len1: %d\n", len);
}

void Side_light_strip(u8 *buf)
{
    spi_send_param *spi2_param = NULL;
    u8 *uart_rx_buffer2 = NULL;

    spi2_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi2_param == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }

    uart_rx_buffer2 = (u8 *)pvPortMalloc(sizeof(u8 *)*(SIDE_LIGHT_STRIP_NUM));

    if(uart_rx_buffer2 == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }
        
    bzero(uart_rx_buffer2,SIDE_LIGHT_STRIP_NUM);
    spi2_param->send_len = SIDE_LIGHT_STRIP_NUM; //待发送数据长度


    for (int i=0;i<(SIDE_LIGHT_STRIP_NUM/3); i++)
    {
        uart_rx_buffer2[i*3+0]=buf[0];
        uart_rx_buffer2[i*3+1]=buf[1];
        uart_rx_buffer2[i*3+2]=buf[2];
    }


    spi2_param->send_data = uart_rx_buffer2; //待发送的数据具体内容
    xQueueSend(spi2_queue_handle,&spi2_param,0);
    uart_rx_buffer2 =NULL; //防止野指针
    spi2_param = NULL;
}


void logic_send_light_msg_ohter(u8 *buf,u16 length)
{
    spi_send_param *spi_param = NULL;
    u8 *uart_rx_buffer = NULL;

    spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }
        uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length));
    if(uart_rx_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
            return;
    }

    bzero(uart_rx_buffer,length);
    spi_param->send_len = length; //待发送数据长度
    memcpy(uart_rx_buffer,buf,length);
    spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
    

    if(get_flag()== 1)
    {
        spi_send_param *spi2_param = NULL;
        u8 *uart_rx_buffer2 = NULL;

        spi2_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
        if (spi2_param == NULL)
        {
            LOG_ERROR("pvPortMalloc create err\n");
            return;
        }

        uart_rx_buffer2 = (u8 *)pvPortMalloc(sizeof(u8 *)*(SIDE_LIGHT_STRIP_NUM));

        if(uart_rx_buffer2 == NULL)
        {
            LOG_ERROR("pvPortMalloc create err\n");
            return;
        }
        
        bzero(uart_rx_buffer2,SIDE_LIGHT_STRIP_NUM);
        spi2_param->send_len = SIDE_LIGHT_STRIP_NUM; //待发送数据长度

        for (int i=0; i<(SIDE_LIGHT_STRIP_NUM/3); i++)
        {
            uart_rx_buffer2[i*3+0]=uart_rx_buffer[0];
            uart_rx_buffer2[i*3+1]=uart_rx_buffer[1];
            uart_rx_buffer2[i*3+2]=uart_rx_buffer[2];
        }

        spi2_param->send_data = uart_rx_buffer2; //待发送的数据具体内容
        xQueueSend(spi2_queue_handle,&spi2_param,0);
        uart_rx_buffer2 =NULL; //防止野指针
        spi2_param = NULL;
    }
    
    xQueueSend(spi_queue_handle,&spi_param,0);
    uart_rx_buffer =NULL; //防止野指针
    spi_param = NULL;
}


void logic_send_light_msg(u8 *buf,u16 length)
{

    memcpy(temp_buf,buf,length); //复制发送的数据用于熄灯渐变效果
    for (int i=0;i<length; i=i+3)
    {
        float brightnessFactor =((float)(get_brighness_level()+1)/(float)101);
        AdjustBrightness(brightnessFactor,&buf[i+0],&buf[i+1],&buf[i+2]);
    }

    spi_send_param *spi_param = NULL;
    u8 *uart_rx_buffer = NULL;

    spi_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
    if (spi_param == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
        return;
    }
        uart_rx_buffer = (u8 *)pvPortMalloc(sizeof(u8 *)*(length));
    if(uart_rx_buffer == NULL)
    {
        LOG_ERROR("pvPortMalloc create err\n");
            return;
    }

    bzero(uart_rx_buffer,length);
    spi_param->send_len = length; //待发送数据长度
    memcpy(uart_rx_buffer,buf,length);
    spi_param->send_data = uart_rx_buffer; //待发送的数据具体内容
    

    if(get_flag()== 1)
    {
        spi_send_param *spi2_param = NULL;
        u8 *uart_rx_buffer2 = NULL;

        spi2_param =(spi_send_param *)pvPortMalloc(sizeof(spi_send_param *));
        if (spi2_param == NULL)
        {
            LOG_ERROR("pvPortMalloc create err\n");
            return;
        }

        uart_rx_buffer2 = (u8 *)pvPortMalloc(sizeof(u8 *)*(SIDE_LIGHT_STRIP_NUM));

        if(uart_rx_buffer2 == NULL)
        {
            LOG_ERROR("pvPortMalloc create err\n");
            return;
        }
        
        bzero(uart_rx_buffer2,SIDE_LIGHT_STRIP_NUM);
        spi2_param->send_len = SIDE_LIGHT_STRIP_NUM; //待发送数据长度

        for (int i=0; i<(SIDE_LIGHT_STRIP_NUM/3); i++)
        {
            uart_rx_buffer2[i*3+0]=uart_rx_buffer[0];
            uart_rx_buffer2[i*3+1]=uart_rx_buffer[1];
            uart_rx_buffer2[i*3+2]=uart_rx_buffer[2];
        }

        spi2_param->send_data = uart_rx_buffer2; //待发送的数据具体内容
        xQueueSend(spi2_queue_handle,&spi2_param,0);
        uart_rx_buffer2 =NULL; //防止野指针
        spi2_param = NULL;
    }
    
    xQueueSend(spi_queue_handle,&spi_param,0);
    uart_rx_buffer =NULL; //防止野指针
    spi_param = NULL;
}

void set_cw_light(u8 *param)
{
    if(get_flag()== 1)
    {
        u32 *uart_rx_buffer = NULL;
        uart_rx_buffer = (u32 *)pvPortMalloc(sizeof(u32*)*(2));
        bzero(uart_rx_buffer,sizeof(u32*)*(2));
        uart_rx_buffer[0]= (u32)(PWM_SUM*param[0]/100*(get_cw_config_brighness()+1)/101*60/100); //W
        uart_rx_buffer[1]= (u32)(PWM_SUM*param[1]/100*(get_cw_config_brighness()+1)/101*60/100); //R
        xQueueSend(pwm_queue_handle,&uart_rx_buffer,0);
        uart_rx_buffer=NULL;
    }
}


void set_cw_light_other(u8 *param)
{
    u32 *uart_rx_buffer = NULL;
    uart_rx_buffer = (u32 *)pvPortMalloc(sizeof(u32*)*(2));
    bzero(uart_rx_buffer,sizeof(u32*)*(2));

    uart_rx_buffer[0]= (u32)(PWM_SUM*param[0]/100*(get_cw_config_brighness()+1)/101*60/100); //W
    uart_rx_buffer[1]= (u32)(PWM_SUM*param[1]/100*(get_cw_config_brighness()+1)/101*60/100); //R
    xQueueSend(pwm_queue_handle,&uart_rx_buffer,0);
    uart_rx_buffer=NULL;
}